package com.liuyang.second.concurrency.chapter2;

public class VolatileTest {

    private volatile static int INIT_VALUE = 0;

    private static int MAX_LIMIT = 5;

    public static void main(String[] args) throws InterruptedException {

        new Thread(() -> {
            int localvalue = INIT_VALUE;
            while (INIT_VALUE < MAX_LIMIT) {
                if (localvalue != INIT_VALUE) {
                    System.out.printf("The Value update to [%d] \n", INIT_VALUE);
                    localvalue = INIT_VALUE;
                }
            }
        },"READER").start();

        new Thread(() -> {
            int localValue = INIT_VALUE;
            while(INIT_VALUE < MAX_LIMIT){
                System.out.printf("Update the value to 【%d】\n",++localValue);
                INIT_VALUE = localValue;
                try {
                    Thread.sleep(1_000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "WRITER").start();
    }
    }
